
using System.Runtime.Serialization;
using System.Data;
using System.ComponentModel;
using static _6.Order_Apply;
using System.Windows.Forms;
namespace _6
{

    public partial class OrderForm : Form
    {
        List<Order_Apply.Order> orders = new List<Order_Apply.Order>();
        Order_Apply.OrderServices service = new Order_Apply.OrderServices();
        public Order_Apply.Order order0;
        public int orderid = 0;
        public int findingid = 0;
        BindingList<Order_Apply.Order> Orders = new BindingList<Order_Apply.Order>();
        
        public OrderForm()
        {
            InitializeComponent();
            dataGridView1.DataSource = Orders;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            @static.sodi = 0;
            Form4 f4 = new Form4();

            f4.ShowDialog();
            if (@static.sodi != 0)
            {
                orderid = @static.sodi;
                bool delorder = false;
                for (int i=Orders .Count -1;i>=0;i--)
                {
                    
                    if (Orders[i].OrderId  ==orderid)
                    {
                        Orders.Remove(Orders[i]);
                        delorder = true;
                        break;
                    }
                }
                if(!delorder)
                {
                    Form3 f3 = new Form3("Order not founded");
                    f3.ShowDialog();
                }
            }


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            { findingid = int.Parse(textBox1.Text); }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            @static.sorder = null;
            Form2 f2 = new Form2();

            f2.ShowDialog();
            if (@static.sorder != null)
            {

                order0 = @static.sorder;
                int j = order0.OrderId;
                bool orderindex = false;

                for(int i=Orders .Count -1;i>=0;i--)
                {
                    if(Orders[i].OrderId == j)
                    {
                        orderindex = true;
                        Orders[i] = order0;
                        break;
                    }
                    
                }
                if (!orderindex)
                {
                    Form3 f3 = new Form3("Order not founded");
                    f3.ShowDialog();
                }
                

                order0 = null;
            }
        }

        public void button1_Click(object sender, EventArgs e)
        {
            @static.sorder = null;
            Form2 f2 = new Form2();

            f2.ShowDialog();
            if (@static.sorder != null)
            {
                order0 = @static.sorder;
                Orders.Add(order0);
                order0 = null;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                string s = findingid.ToString();
                if (row.Cells[1].Value != null && row.Cells[1].Value.ToString() == s)
                {
                    dataGridView1.ClearSelection();
                    row.Selected = true;
                    dataGridView1.FirstDisplayedScrollingRowIndex = row.Index;
                    break;
                }
            }
        }
    }

    public class Order_Apply
    {

        public abstract class Information
        {
            public abstract bool Equal(Object obj);
            public abstract new void ToString();
        }
        
        public class Custumor : Information
        {

            public string name { get; set; }

            public int phone_number { get; set; }

            public string home { get; set; }
            public Custumor(string name, int phone_number, string home)
            {
                this.name = name;
                this.phone_number = phone_number;
                this.home = home;
            }
            public Custumor() { }


            public override bool Equal(Object obj)
            {
                return obj is Custumor custumor && name == custumor.name && phone_number == custumor.phone_number && home == custumor.home;

            }
            public override void ToString()
            {
                Console.WriteLine("The name of the custumor is " + name);
                Console.WriteLine("The phone number of the custumor is " + phone_number);
                Console.WriteLine("The home of the custumor is " + home);
            }
        }
        public class Product : Information
        {
            public string name { get; set; }
            public decimal unitprice { get; set; }
            public int quantity { get; set; }
            public decimal Amount { get; set; }
            
            public Product(string name, decimal unitprice, int quantity)
            {
                this.name = name;
                this.unitprice = unitprice;
                this.quantity = quantity;
                this.Amount = unitprice * quantity;
            }
            public Product() { }
            public override bool Equal(object obj)
            {
                return obj is Product product && name == product.name && unitprice == product.unitprice && quantity == product.quantity;
            }
            public override void ToString()
            {
                Console.WriteLine("The " + name + " is per " + unitprice + "$");
                Console.WriteLine("The quantity of the product is " + quantity);
                Console.WriteLine(Amount);
            }
        }
        
        public class Order:Information
        {
            
            [DataMember]
            public int OrderId { get; set; }
            [DataMember ]
            public string custumorname
            {
                get;
                set;
            }
            public List<OrderDetail> OrderDetails = new List<OrderDetail>();
            [DataMember]
            public decimal TotalAmount
            {
                get
                {
                    decimal total = 0;
                    foreach (OrderDetail orderdetail in OrderDetails)
                    {
                        total += orderdetail.product.Amount;
                    }
                    return total;
                }
            }
            public Custumor custumor;
            public Order(Custumor custumor, int orderid)
            {
                this.custumor = custumor;
                this.OrderId = orderid;
                this.custumorname = custumor.name;
            }
            public Order() 
            {
            }

            public override bool Equal(object obj)
            {
                return obj is Order order && OrderId == order.OrderId && custumor == order.custumor && OrderDetails == order.OrderDetails;

            }
            public override void ToString()
            {
                Console.WriteLine("The ID of the Order is " + OrderId);
                custumor.ToString();
                foreach (OrderDetail orderDetail in OrderDetails)
                {
                    orderDetail.ToString();
                }
                Console.WriteLine("The total amount is " + TotalAmount);
            }

        }
        public class OrderDetail : Information
        {
            [DataMember]
            public Product product { get; set; }

            public OrderDetail(Product product)
            {
                this.product = product;
            }
            public OrderDetail() { }


            public override bool Equal(object obj)
            {
                return obj is OrderDetail detail && detail.product.Equal(obj);
            }
            public override void ToString()
            {
                product.ToString();
            }

        }
        public class OrderServices
        {
           
            public void AddOrder(Order order,List <Order>  orders)
            {
                bool result = true;
                foreach (Order order1 in orders)
                {
                    if (order.Equal(order1))
                    {
                        result = false;
                    }
                }
                if (result)
                {
                    orders.Add(order);
                }
                else 
                {
                    Form3 f3 = new Form3("Order has been exsiting");
                    f3.ShowDialog();
                }
            }

            public void RemoveOrder(int orderId, List<Order> orders)
            {
                Order? o = orders.FirstOrDefault(o => o.OrderId == orderId);
                if (o == null)
                {
                    Form3 f3 = new Form3("Order not founded");
                    f3.ShowDialog();
                }
                else
                {
                    orders.Remove(o);
                }
            }

            public void UpdateOrder(int orderId, Order updateorder,List<Order> orders)
            {
                var OrderIndex = orders.FindIndex(o => { return o.OrderId == orderId; });
                if (OrderIndex == -1)
                {
                    Form3 f3 = new Form3("Order not founded");
                    f3.ShowDialog();
                }
                else
                {
                    orders[orderId] = updateorder;
                }
            }

            public void SortByTotalAmount(List<Order> orders)
            {
                List<Order> neworders = (List<Order>)orders.OrderByDescending(o => o.TotalAmount);
                orders = neworders;
            }

            public void SortByOrderId(List<Order> orders)
            {
                List<Order> neworders = (List<Order>)orders.OrderByDescending(o => o.OrderId);
                orders = neworders;
            }

            public void SortByCustumorName(List<Order> orders)
            {
                List<Order> neworders = (List<Order>)orders.OrderByDescending(o => o.custumor.name.First());
                orders = neworders;
            }

            public void ShowInformationBySort(int i, List<Order> orders)
            {
                List<Order> neworders = (List<Order>)orders.OrderByDescending(o => o.TotalAmount);
                switch (i)
                {
                    case 1:
                        {
                            foreach (Order no in neworders)
                            {
                                Console.WriteLine(no.OrderId);
                            }
                        }
                        break;
                    case 2:
                        {
                            foreach (Order no in neworders)
                            {
                                no.custumor.ToString();
                            }
                        }
                        break;
                    case 3:
                        {
                            foreach (Order no in neworders)
                            {
                                foreach (OrderDetail orderDetail in no.OrderDetails)
                                {
                                    orderDetail.ToString();
                                }
                            }
                        }
                        break;
                    case 4:
                        {
                            foreach (Order no in neworders)
                            {
                                Console.WriteLine(no.TotalAmount);
                            }
                        }
                        break;
                    default:
                        break;
                }
            }

            public List<Order> GetAllOrder(List<Order> orders)
            {
                return orders;
            }
        }
    }
}
